﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Kandy_K_Issue_Approve : System.Web.UI.Page
{

    string order;
    string proname;
    decimal qty;
    public static int resultrr;
    string Stockcode;

    protected void Page_Load(object sender, EventArgs e)
    {

        Session["current"] = "GTN Approve";//current page
        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }

        }
        else
        {
            Response.Redirect("index.aspx");
        }


       

        string b = Request.QueryString.ToString();
        if (b != "")
        {
            ASPxLabel5.Visible = true;
            //ASPxLabel4.Visible = false;
            ASPxLabel5.Text = "Add Stock to Food city Stock Sucessfully!!";
            decimal grnlast = Bestlife.ERP.BusinessRules.B_Kandy.lastnotappro_issu();
            DropDownList1.SelectedIndex = -1;
            DropDownList1.Text = grnlast.ToString();
            ASPxGridView2.DataBind();
        }
        else
        {


            if (!IsPostBack)
            {
             
                decimal grnlast = Bestlife.ERP.BusinessRules.B_Kandy.lastnotappro_issu();
                DropDownList1.SelectedIndex = -1;
                DropDownList1.Text = grnlast.ToString();

                ASPxGridView2.DataBind();



            }
        }

       

    }
       
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
       
        List<Bestlife.ERP.Entities.E_Issue_Ap> listgrn = new List<Bestlife.ERP.Entities.E_Issue_Ap>();

        int i = ASPxGridView2.VisibleRowCount;
        int result;
        List<Object> itemid1 = ASPxGridView2.GetSelectedFieldValues("Pro_ID");
        List<Object> itemid = ASPxGridView2.GetSelectedFieldValues("Issue_ID");
        List<Object> itemid2 = ASPxGridView2.GetSelectedFieldValues("Qty");

        List<Object> itemid4 = ASPxGridView2.GetSelectedFieldValues("Sto_ID");
        int j = itemid.Count;
        string user = Session["username"].ToString();
        if (DropDownList2.SelectedValue != "")
        {
            if (DropDownList2.SelectedValue == "YES")
            {
                string approval = DropDownList2.SelectedValue;
                if (itemid.Count == 0 || itemid1.Count == 0 || itemid2.Count == 0 || itemid4.Count == 0)
                {
                    ASPxLabel2.Visible = true;
                    ASPxLabel2.Text = "Please select order items ";
                }
                else
                {
                    foreach (object selectitemid in itemid)
                    {
                        order = selectitemid.ToString();

                    }
                    foreach (object selectitemid1 in itemid1)
                    {
                        proname = selectitemid1.ToString();

                    }
                    foreach (object selectitemid2 in itemid2)
                    {
                        qty = decimal.Parse(selectitemid2.ToString());

                    }

                    foreach (object selectitemid4 in itemid4)
                    {
                        Stockcode = selectitemid4.ToString();

                    }
                    for (int k = 0; k < itemid.Count; k++)
                    {
                        Bestlife.ERP.Entities.E_Issue_Ap grn = new Bestlife.ERP.Entities.E_Issue_Ap();
                        grn.Stor_ID = itemid4[k].ToString();

                        grn.Issu_ID1 = itemid[k].ToString();
                        grn.Pro_id = itemid1[k].ToString();
                        grn.Qty = decimal.Parse(itemid2[k].ToString());
                        listgrn.Add(grn);

                    }
                 
                }
                string approve = "YES";
                if (i == j)
                {


                    result = Bestlife.ERP.BusinessRules.B_Kandy.allissuapprove(listgrn, user, approve, itemid.Count);
                    if (result ==1)
                    {
                        Response.Redirect("K_Issue_Approve.aspx?b=1");
                    }
                    else if (result == 3)
                    {
                        ASPxLabel2.Visible = true;
                        ASPxLabel2.Text = "This Item not available in Main Store";
                    }
                    else
                    {
                        ASPxLabel2.Visible = true;
                        ASPxLabel2.Text = "Error In the Process";
                    }


                }
                else
                {
                    string approve1;
                    int countrow = ASPxGridView2.VisibleRowCount;
                    int listcount = listgrn.Count;
                    if (countrow == listcount)
                    {
                        approve1 = "YES";
                    }
                    else
                    {
                        approve1 = "HALF";
                    }

                    foreach (Bestlife.ERP.Entities.E_Issue_Ap listgn in listgrn)
                    {

                        resultrr = Bestlife.ERP.BusinessRules.B_Kandy.approve_issu(listgn, user, approve1, itemid.Count);
                    }

                }
                if (resultrr == 1)
                {
                    Response.Redirect("K_Issue_Approve.aspx?b=1");
                }
                else if (resultrr == 3)
                {
                    ASPxLabel2.Visible = true;
                    ASPxLabel2.Text = "This Item not available in Main Store";
                }
                else
                {
                    ASPxLabel2.Visible = true;
                    ASPxLabel2.Text = "Error In the Process";
                }
            }

           
        }
        else
        {
            ASPxLabel2.Visible = true;
            ASPxLabel2.Text = "Please select one option for approve ";
        }




    }

  
  
}

